package logic

import (
	"context"
	"fmt"
	"user/models/mysql"
	"user/models/redis"

	"user/internal/svc"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartLogic {
	return &CartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartLogic) Cart(in *user.CartRequest) (*user.CartResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Good{}
	err := g.GoodsFind(int(in.GoodsId))
	if err != nil {
		logx.Error("商品不正确")
		return nil, fmt.Errorf("商品查询不正确")
	}
	if g.ID == 0 {
		logx.Error("商品查询不正确")
		return nil, fmt.Errorf("商品查询不正确")
	}
	err = redis.CartAdd(in.GoodsId, in.UserId, in.Num, in.Stock, in.Title, in.CartCover)
	if err != nil {
		return nil, err
	}

	return &user.CartResponse{}, nil
}
